apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "fastnginx.fullname" . }}
  labels:
    {{- include "fastnginx.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "fastnginx.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        htmlchecksum: {{ include (print $.Template.BasePath "/configmap-html.yaml") .  | sha256sum }}
        confdchecksum: {{ include (print $.Template.BasePath "/configmap-confd.yaml") .  | sha256sum }}
        nginxchecksum: {{ include (print $.Template.BasePath "/configmap-nginx.yaml") .  | sha256sum }}
      {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "fastnginx.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: init-configs
          image: {{ .Values.initContainer.image }}
          imagePullPolicy: {{ .Values.initContainer.pullPolicy }}
          command:
            - sh
            - -c
            - |
              mkdir /var/cache/nginx
              chown {{ .Values.securityContext.runAsUser }}:{{ .Values.securityContext.runAsUser }} /var/cache/nginx
              chmod 775 /var/cache/nginx
          volumeMounts:
            - name: cache
              mountPath: /var/cache/
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPort }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
          - name: confd
            mountPath: /etc/nginx/conf.d/
          - name: nginxconf
            mountPath: /etc/nginx/nginx.conf
            readOnly: true
            subPath: nginx.conf
          - name: cache
            mountPath: /var/cache/
          - name: run
            mountPath: /run
          - name: html
            mountPath: /usr/share/nginx/html
      volumes:
      - name: confd
        configMap:
          name: {{ include "fastnginx.fullname" . }}-conf
      - name: nginxconf
        configMap:
          name: {{ include "fastnginx.fullname" . }}-nginx
      - name: cache
        emptyDir: {}
      - name: run
        emptyDir:
          medium: Memory
      - name: html
        configMap:
          name: {{ include "fastnginx.fullname" . }}-html
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
